package com.dong.dongweather.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.util.ArrayList;

public class DataBaseAdapter {

    private DatabaseHelper dbHelper;

    public DataBaseAdapter(Context context) {
        this.dbHelper = new DatabaseHelper(context);
    }



    //注册信息插入数据库
    public void add(User user){
        String sql = "insert into user(userName,password) values(?,?)";
        Object[] args = {user.getUsername(),user.getPassword()};
        //获取操作数据库的工具类
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.execSQL(sql,args);
        db.close();
    }



   //对账号进行查询
    public User find(String username){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String sql = "select userName,password from user where userName = ?";
        Cursor cursor = db.rawQuery(sql,new String[]{username});
        User user = null;
        if(cursor.moveToNext()){
            user = new User();
            user.setUsername(cursor.getString(cursor.getColumnIndexOrThrow("userName")));
            user.setPassword(cursor.getString(cursor.getColumnIndexOrThrow("password")));
        }
        cursor.close();
        db.close();
        return user;
    }


}
